package com.ltwc.video;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.View;


import com.ltwc.lib.R;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.model.VideoOptionModel;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoViewBridge;

import java.util.ArrayList;
import java.util.List;

import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Description：
 * Created on 2024/1/31 18:38
 * Organization:华云
 * author：AHuangSHang
 **/
public class TVVideoPlayer extends BasePlayerlView {
    public TVVideoPlayer(Context context) {
        super(context);
    }

    public TVVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TVVideoPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context);
    }

    @Override
    protected boolean backFromFull(Context context) {
        return false;
    }

    @Override
    protected void releaseVideos() {

    }

    @Override
    public GSYVideoViewBridge getGSYVideoManager() {
        GSYVideoManager.instance().initContext(getContext().getApplicationContext());
        VideoOptionModel videoOptionModel =
                new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 50);
        // 设置网络连接超时时间
        VideoOptionModel videoOptionModel1 =
                new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "timeout", "30000");
        // 启用 IPv6 解析
        VideoOptionModel videoOptionModel2 =
                new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "http-proxy-host", "::");
        List<VideoOptionModel> list = new ArrayList<>();
        list.add(videoOptionModel);
        list.add(videoOptionModel1);
        list.add(videoOptionModel2);
        //设置秒开效果
        list.add(new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 1));// 设置播放前的探测时间 1,达到首屏秒开效果
        //list.add(new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 1024 * 32));// 播放前的探测Size，默认是1M, 改小一点会出画面更快
        //list.add(new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1));//每处理一个packet之后刷新io上下文
        //list.add(new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48));//设置是否开启环路过滤: 0开启，画面质量高，解码开销大，48关闭，画面质量差点，解码开销小
        GSYVideoManager.instance().setOptionModelList(list);
        return GSYVideoManager.instance();
    }

    @Override
    public int getLayoutId() {
        return R.layout.video_layout_standard;
    }

    public void start(String url, boolean cacheWithPlay, String title) {
        setUp(url, cacheWithPlay, title);
        startPlayLogic();
    }

    @Override
    public void startPlayLogic() {
        if (mVideoAllCallBack != null) {
            mVideoAllCallBack.onClickStartThumb(mOriginUrl, mTitle, this);
        }
        prepareVideo();
    }
    //这个方法没有用，返回的都是0，有机会研究下为什么
    @Override
    public void onBufferingUpdate(int percent) {
    }

    @Override
    public void onBackFullscreen() {

    }

    @Override
    protected void setSmallVideoTextureView() {

    }

   /* boolean isSurfaceAvailable;

    @Override
    protected void setProgressAndTime(long progress, long secProgress, long currentTime, long totalTime, boolean forceChange) {
        super.setProgressAndTime(progress, secProgress, currentTime, totalTime, forceChange);
        if (getCurrentState() == CURRENT_STATE_PLAYING || getCurrentState() == CURRENT_STATE_PLAYING_BUFFERING_START) {
            if (!isSurfaceAvailable) {
                try {
                    requestLayoutLoop(this);
                } catch (Exception e) {
                    onCompletion();
                }
                return;
            }
        }
    }

    @Override
    protected void addTextureView() {
        super.addTextureView();
        isSurfaceAvailable = false;
    }

    @Override
    public void onSurfaceAvailable(Surface surface) {
        super.onSurfaceAvailable(surface);
        isSurfaceAvailable = true;
    }


    public void requestLayoutLoop(Object obj) {
        if (obj == null) {
            return;
        }
        if (!(obj instanceof View)) {
            return;
        }
        View view = (View) obj;
        view.requestLayout();
        requestLayoutLoop(view.getParent());
    }*/
}
